
DEFINE STREAM bf.      

DEFINE VARIABLE iYear AS INTEGER.
DEFINE VARIABLE iMonth AS INTEGER.

DEFINE VARIABLE sDate AS DATE.
DEFINE VARIABLE eDate AS DATE.

DEFINE VARIABLE csset AS CHARACTER.

DEFINE VARIABLE yMonth AS CHARACTER.
DEFINE VARIABLE fMonth AS CHARACTER.

DEFINE VARIABLE mtl LIKE sct_mtl_ll.
DEFINE VARIABLE lbr LIKE sct_lbr_tl.

DEFINE BUFFER sct_1st FOR sct_det.
DEFINE BUFFER sct_std FOR sct_det.

eDate = DATE(MONTH(TODAY), 1, YEAR(TODAY)) - 1.
sDate = DATE(MONTH(eDate), 1, YEAR(eDate)).
/*
sDate = DATE(9,1,2009).
eDate = DATE(9,30,2009).
*/

iYear = YEAR(sDate).
iMonth = MONTH(sDate).

csset = STRING(iYear, "9999") + "-" + STRING(iMonth, "99") + "M".

fMonth = STRING(iYear, "9999") + "M" + STRING(iMonth, "99").

yMonth = IF iMonth >= 4 THEN STRING(iYear, "9999") + "F" + STRING(iMonth - 3, "99") ELSE STRING(iYear - 1, "9999") + "F" + STRING(iMonth + 9, "99").

OUTPUT STREAM bf TO VALUE("\\192.168.12.12\ftp$\R3032\R3032.csv").

EXPORT STREAM bf DELIMITER "," "yMonth" "fMonth" "Depart" "MfgLine" "WoNbr" "Part" "RctQty" "RctDate" "Material" "Labor" "TotCost".

FOR EACH tr_hist FIELDS(tr_nbr tr_part tr_type tr_effdate tr_qty_chg tr_site) NO-LOCK WHERE tr_type = "RCT-WO" AND tr_effdate >= sDate AND tr_effdate <= eDate:
    
    FIND FIRST wo_mstr NO-LOCK WHERE wo_nbr = tr_nbr NO-ERROR.
    
    IF NOT AVAILABLE wo_mstr OR wo_type <> "" OR wo__chr01 > "72A" THEN NEXT.
    
    FIND FIRST sct_1st NO-LOCK WHERE sct_1st.sct_sim = "StdFirst" AND sct_1st.sct_part = tr_part AND sct_1st.sct_site = tr_site NO-ERROR.
    FIND FIRST sct_std NO-LOCK WHERE sct_std.sct_sim = "Standard" AND sct_std.sct_part = tr_part AND sct_std.sct_site = tr_site NO-ERROR.

    FIND FIRST sct_det NO-LOCK WHERE sct_det.sct_sim = csset AND sct_det.sct_part = tr_part AND sct_det.sct_site = tr_site NO-ERROR.

    mtl = IF AVAILABLE sct_det AND sct_det.sct_mtl_tl + sct_det.sct_mtl_ll > 0 THEN sct_det.sct_mtl_tl + sct_det.sct_mtl_ll ELSE 
          IF AVAILABLE sct_std THEN sct_std.sct_mtl_tl + sct_std.sct_mtl_ll + sct_std.sct_sub_tl + sct_std.sct_sub_ll ELSE 0.0.
    
    lbr = IF AVAILABLE sct_1st AND sct_1st.sct_lbr_tl > 0 THEN sct_1st.sct_lbr_tl ELSE IF AVAILABLE sct_std THEN sct_std.sct_lbr_tl ELSE 0.0 +
          IF AVAILABLE sct_det AND sct_det.sct_lbr_ll > 0 THEN sct_det.sct_lbr_ll ELSE IF AVAILABLE sct_std THEN sct_std.sct_lbr_ll ELSE 0.0 +
          IF AVAILABLE sct_std THEN sct_std.sct_bdn_tl + sct_std.sct_bdn_ll ELSE 0.0.

    /**/
    EXPORT STREAM bf DELIMITER "," 
        yMonth
        fMonth
        SUBSTRING(wo__chr01, 1, 3) + "000"
        wo__chr01
        wo_nbr
        wo_part
        tr_qty_chg
        tr_effdate
        mtl
        lbr
        (mtl * 1.17 + lbr * 1.1768) * 1.06
        .
END.

OUTPUT STREAM bf CLOSE.


